package com.example.demo.repository;

import com.example.demo.entity.Category;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import javax.transaction.Transactional;
import java.util.List;

public interface CategoryRepository extends JpaRepository<Category, Long> {
    //自己添加需要的
    //方式1：查询方法名使用JPA的命名规则（功能会自动实现）
    List<Category> findByNameLikeOrderByIdDesc(String name);

    //方式2：使用JPQL语言
    @Transactional
    @Modifying
    @Query("update Category b set b.name = ?1 where b.id = ?2")
    int updateCategory(String name,  Long category_id);
    //方式3：使用原生的sql语言
    @Transactional
    @Modifying
    @Query(value = "delete from category where name =?1", nativeQuery = true)
    int deleteByName(String name);

    @Transactional
    @Modifying
    @Query(value="insert into category set id =?1,name =?2",nativeQuery = true)
    int addCatrgory(Long id,String name);

    @Transactional
    @Modifying
    @Query(value="delete from category where id =?1",nativeQuery = true)
    int deleteByCatId(Long id);

    @Transactional
    @Modifying
    @Query(value="select id ,name from category where id like concat('%',?1,'%')",nativeQuery = true)
    List<Category> findCategoriesByById(Long id);
}
